using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace dmWS
{
	/// <summary>
	/// Summary description for basket.
	/// </summary>
	public class basket : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid datagrid;
		KTManage kt;
		protected System.Web.UI.WebControls.Label eti;
		DataTable dati;
		protected System.Web.UI.WebControls.Label feedback;
		protected System.Web.UI.WebControls.Label titleDel;
		protected System.Web.UI.WebControls.Button gotoDocs;
		Translation traduttore;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			kt = new KTManage();
			traduttore = new Translation(Request.ServerVariables["http_accept_language"].ToString(), Server.MapPath("sentences.xml"));
		    titleDel.Text = traduttore.getText("titleDel");
			gotoDocs.Text = traduttore.getText("gotoDocs");

			try 
			{
				string action = Request.Params["action"].ToString();
				string docid = Request.Params["document"].ToString();
				
				if (action == "expunge")
				{
					string ask = Request.Params["ask"].ToString();
					if (ask == "true")
					{
						string host = System.Configuration.ConfigurationSettings.AppSettings["uploadUrl"].ToString();
						string pageyes = "/basket.aspx?action=expunge&ask=false&document="+docid ;
						string pageno = "/basket.aspx?action=expunge";

						string urlyes = host+pageyes;
						string urlno = host+pageno;
						string message = traduttore.getText("confirmExpunge");

						string scriptCode = "<script type=\"text/javascript\">confirmDelete('"+urlyes+"', '"+urlno+"', '"+message+"' );</script>";

						Page.RegisterClientScriptBlock("confirmScript", scriptCode);
					}
					else
					{
						kt.expungeKTDocument(docid);
						feedback.Text = traduttore.getText("expunged");
					}
				}
				if (action == "resume")
				{
					kt.changeStatus(docid, "1");
					feedback.Text = traduttore.getText("resumed");
				}
			}
			catch (Exception ex)
			{
			}

			dati = getGrid();
			datagrid.DataSource = dati;
			datagrid.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.gotoDocs.Click += new System.EventHandler(this.gotoDocs_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion




		DataTable getGrid()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add(new DataColumn("id"));
			dt.Columns["id"].DataType = System.Type.GetType("System.String");
			dt.Columns.Add(new DataColumn("name"));
			dt.Columns["name"].DataType = System.Type.GetType("System.String");
			dt.Columns.Add(new DataColumn("expunge"));
			dt.Columns["expunge"].DataType = System.Type.GetType("System.String");
			dt.Columns.Add(new DataColumn("resume"));
			dt.Columns["resume"].DataType = System.Type.GetType("System.String");
			/*dt.Columns.Add(new DataColumn("versionid"));
			dt.Columns["versionid"].DataType = System.Type.GetType("System.String");
			dt.Columns.Add(new DataColumn("date"));
			dt.Columns["date"].DataType = System.Type.GetType("System.String");
			dt.Columns.Add(new DataColumn("owner"));
			dt.Columns["owner"].DataType = System.Type.GetType("System.String");*/

			string folder = System.Configuration.ConfigurationSettings.AppSettings["enabFolder"].ToString();
			
			string query = "select document_content_version.filename, documents.id as idi, "+
				"document_metadata_version.name from document_content_version join "+
				"documents on (document_content_version.document_id = documents.id) "+
				"join document_metadata_version on (document_content_version.id = document_metadata_version.content_version_id) "+
				"where document_content_version.id = (select min(id) from  document_content_version where document_id=documents.id) and "+
				" documents.folder_id="+folder+" and documents.status_id=3 order by idi";

			string indi = System.Web.HttpContext.Current.Server.MapPath("logShowBasket.txt");
			FileInfo tlog = new FileInfo(indi);
			StreamWriter scrivi = tlog.CreateText();
			scrivi.WriteLine(query);
			scrivi.Close();

			MySql.Data.MySqlClient.MySqlDataReader rea = kt.Db.ExecuteQuery(query);
			
			string ktfsWeb = System.Configuration.ConfigurationSettings.AppSettings["ktfsWeb"].ToString();
			bool hasItems = false;
			while (rea.Read())
			{
				hasItems = true;
				DataRow riga = dt.NewRow();
				string filename = rea["filename"].ToString();
				string name = rea["name"].ToString();
				string docid = rea["idi"].ToString();
				riga[0] = docid;
				//url sulla MIA macchina
				string url = "<a href=\""+ktfsWeb+filename+"\">"+name+"</a>";
				riga[1] = replaceUrl(url);

				riga[2] = "<a href=\"basket.aspx?action=expunge&ask=true&document="+docid+"\">"+traduttore.getText("expunge")+"</a>";

				riga[3] = "<a href=\"basket.aspx?action=resume&document="+docid+"\">"+traduttore.getText("resume")+"</a>";
				/*riga[2] = contatore.ToString();
				contatore++;
				riga[3] = rea["version_created"].ToString();
				riga[4] = rea["name"].ToString();*/
				dt.Rows.Add(riga);
			}
			rea.Close();
			kt.Db.CloseConnection();

			if (!hasItems)
			{
				datagrid.Visible = false;
				eti.Text = "Basket is empty";
			}

			return dt;
		}

		private string replaceUrl(string url)
		{
			string extName = System.Configuration.ConfigurationSettings.AppSettings["serverExtName"].ToString();
			string intName = System.Configuration.ConfigurationSettings.AppSettings["serverIntName"].ToString();
			string ris = url.Replace(intName, extName);
			return ris;
		}

		private void gotoDocs_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("documents.aspx", false);
		}
	}
}
